package com.bristua.mall.order.biz;

import com.bristua.mall.common.exception.BizException;
import com.bristua.mall.common.utils.DateUtil;
import com.bristua.mall.common.utils.SequenceUtil;
import com.bristua.mall.order.dao.TOrderMapper;
import com.bristua.mall.order.dao.TOrderPayFlowMapper;
import com.bristua.mall.order.enums.OrderCode;
import com.bristua.mall.order.model.TOrder;
import com.bristua.mall.order.model.TOrderPayFlow;
import com.bristua.mall.order.model.condition.TOrderPayFlowExample;
import com.bristua.mall.user.dao.TUserMapper;
import com.bristua.mall.user.model.TUser;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/19.
 */
@Service
@Transactional
public class WxAppPayOrderAsyncCallbackBiz {
    @Resource
    private TOrderMapper tOrderMapper;

    @Resource
    private TOrderPayFlowMapper tOrderPayFlowMapper;

    @Resource
    private TUserMapper tUserMapper;


    @Async
    public void updateOrderPayStatus(Map<String,String> map){
        if(map!=null){
            String transaction_id = map.get("transaction_id");

            TOrderPayFlowExample tOrderPayFlowExample = new TOrderPayFlowExample();
            tOrderPayFlowExample.createCriteria().andTransactionIdEqualTo(transaction_id);
            List<TOrderPayFlow> tOrderPayFlows = tOrderPayFlowMapper.selectByExample(tOrderPayFlowExample);
            if(tOrderPayFlows==null || tOrderPayFlows.size()==0){
                String out_trade_no = map.get("out_trade_no");
                TOrder tOrder = tOrderMapper.selectByPrimaryKey(out_trade_no);

                if(tOrder==null){
                    throw new BizException(OrderCode.ORDER_NOT_FIND.getCode(),OrderCode.ORDER_NOT_FIND.getMsg());
                }

                String total_fee = map.get("total_fee");
                if(!total_fee.equals(tOrder.getOrderPrice())){
                    throw new BizException(OrderCode.ORDER_AMT_EXCEPTION.getCode(),OrderCode.ORDER_AMT_EXCEPTION.getMsg());
                }


                tOrder.setPayStaus("2");
                tOrder.setSendStatus("1");
                tOrder.setPayType(1);
                tOrder.setPayFee(total_fee);
                String time_end = map.get("time_end");
                tOrder.setPayTime(time_end);
                tOrder.setTransactionNo(transaction_id);
                tOrderMapper.updateByPrimaryKeySelective(tOrder);

                TOrderPayFlow tOrderPayFlow = new TOrderPayFlow();
                tOrderPayFlow.setCreateTime(DateUtil.getDateTime3());
                tOrderPayFlow.setOrderId(out_trade_no);
                tOrderPayFlow.setOrderPayId(SequenceUtil.nextId());
                tOrderPayFlow.setPayTime(time_end);
                tOrderPayFlow.setTotalFee(total_fee);
                tOrderPayFlow.setUserId(tOrder.getUserId());
                tOrderPayFlow.setTransactionId(transaction_id);
                tOrderPayFlow.setPayType(1);
                TUser tUser = tUserMapper.selectByPrimaryKey(tOrder.getUserId());
                tOrderPayFlow.setUserName(tUser.getUserName());
                tOrderPayFlowMapper.insert(tOrderPayFlow);
            }

        }
    }
}
